//
//  NewsViewController.m
//  FNer
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 刘志强. All rights reserved.
//

#import "NewsViewController.h"
#import "MostNewTableViewCell.h"
#import "DetailHtworld.h"
#import "RESideMenu.h"
@interface NewsViewController ()<UITableViewDelegate,UITableViewDataSource>


@property(nonatomic,retain)UITableView *mostNewTV;
@property(nonatomic,retain)NSMutableArray *htworldArray;
@property(nonatomic,assign)BOOL isUpLoading;
@property(nonatomic,retain)UILabel *titleLabel;

@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.isUpLoading = NO;
    
    //左方返回按钮
    UIImage *leftImage = [UIImage imageNamed:@"导航栏_button_back_用于白和灰.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    
    //标题
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    self.titleLabel.text = @"最新";
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.font = [UIFont systemFontOfSize:18 * XWIDTH];
    self.navigationItem.titleView = self.titleLabel;
    
    
    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:@"nighting"];
    if ([string isEqualToString:@"yes"]) {
#warning 改的颜色
        self.titleLabel.textColor = [UIColor blackColor];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColoring:) name:@"changeColoring" object:nil];
    
    //创建最新列表
    [self setmostNewTV];
    
    //下拉刷新
    [self addHeaderRefresh];

    
}

//下拉刷新
-(void)addHeaderRefresh
{
    self.mostNewTV.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUpLoading = NO;
        [self getMostNewData];
        
    }];
    [self.mostNewTV.header beginRefreshing];
}


//点击左方返回按钮触发
-(void)leftButtonAction
{
       [self.sideMenuViewController presentLeftMenuViewController];
}


//创建最新列表
-(void)setmostNewTV
{
    self.mostNewTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375 * XWIDTH, 667 * XHIGHT) style:UITableViewStylePlain];
    self.mostNewTV.backgroundColor = [UIColor colorWithRed:231 / 255.0 green:231 / 255.0 blue:231 / 255.0 alpha:1];
    self.mostNewTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.mostNewTV];
    self.mostNewTV.delegate = self;
    self.mostNewTV.dataSource = self;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.htworldArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    MostNewTableViewCell *MostNEWcell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (MostNEWcell == nil) {
        MostNEWcell = [[MostNewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    MostNEWcell.contentView.backgroundColor = [UIColor colorWithRed:231 / 255.0 green:231 / 255.0 blue:231 / 255.0 alpha:1];
    
    MostNEWcell.aDetailHT = [self.htworldArray objectAtIndex:indexPath.row];
    
    return MostNEWcell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0 * XHIGHT;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 500 * XHIGHT;
}

//获取最新信息
-(void)getMostNewData
{
    NSString *url_string = [NSString stringWithFormat:@"http://api.imzhitu.com/ztworld/ztworld_queryLatestWorldV3"];
    
    //有body体的
    NSDictionary *body = @{@"maxId":@"0",@"start":@"1",@"limit":@"15",@"trimExtras":@"false",@"commentLimit":@"2",@"likedLimit":@"10"};

    
    AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
#warning 最新COOKIE
    //    //有Cookie的
    [manager.requestSerializer setValue:@"IMZHITU_REMEMBER_ME_COOKIE=QjFuK0Nmc1RleEl4VTVvSW02a0R4UT09OkJreEI0c1BuYkI4YWRKV3JocitTd0E9PQ; JSESSIONID=444A1130F5376C095EF2207F505A7B0B; SERVERID=f2bba00007611e8b21e74f5166ce6604|1444699403|1444696238" forHTTPHeaderField:@"Cookie"];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager POST:url_string parameters:body success:^(NSURLSessionDataTask *operation, id responseObject) {
        self.htworldArray = [NSMutableArray array];
        NSMutableDictionary *bigDic = responseObject;
        NSMutableArray *bigArray = [bigDic objectForKey:@"htworld"];
        for (NSMutableDictionary *dic in bigArray) {
            DetailHtworld *aDetailHT = [[DetailHtworld alloc] init];
            [aDetailHT setValuesForKeysWithDictionary:dic];
            aDetailHT.aUser = [[BaseModel alloc] init];
            [aDetailHT.aUser setValuesForKeysWithDictionary:aDetailHT.userInfo];
            [self.htworldArray addObject:aDetailHT];
        }
        
        [self.mostNewTV.header endRefreshing];
        [self.mostNewTV reloadData];
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        
        NSLog(@"没有网络");
        [self.mostNewTV.header endRefreshing];
        
    }];
}

//点击最新列表触发事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.mostNewTV deselectRowAtIndexPath:indexPath animated:YES];
    
 //   NewDetailViewController *newDetailVC = [[NewDetailViewController alloc] init];
 //   [self.navigationController pushViewController:newDetailVC animated:NO];
    
 //   newDetailVC.ahtWorld = [self.htworldArray objectAtIndex:indexPath.row];
}

-(void)changeColoring:(NSNotification *)notification
{
#warning 改的颜色
    self.titleLabel.textColor = [UIColor blackColor];
    
    NSString *string = [notification object];
    if ([string isEqualToString:@"no"]) {
        
#warning 原有颜色
        self.titleLabel.textColor = [UIColor whiteColor];
    }
    //保存当前状态值
    [[NSUserDefaults standardUserDefaults] setObject:string forKey:@"nighting"];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
